function toLog( str )
{
    console.log( str );
}

function getRandom( max )
{
  return Math.floor( Math.random() * ( max - 0 + 1 ) ) + 0;
}

function intersection( first, second )
{
    return ( ( first.x < second.x + second.width  ) && ( first.x + first.width  > second.x ) && 
             ( first.y < second.y + second.height ) && ( first.y + first.height > second.y ) )
}

function toTag( user, id1, id2 )
{
    if ( intersection( user[id1], user[id2] ) && user[id1].catching )
    {
        user[id2].catching = true;
        user[id1].catching = false;
    }
}

function keyPress( keyCode, id )
{   
    switch( keyCode )
    {
      case key.up:
        user[id].y = ( user[id].y > 0 ) ? user[id].y - user[id].speed : 0;
        break;
      case key.down:
        user[id].y = ( user[id].y < 440 ) ? user[id].y + user[id].speed : 440;
        break;
      case key.left:
        user[id].x = ( user[id].x > 0   ) ? user[id].x - user[id].speed : 0;
        break;
      case key.right:
        user[id].x = ( user[id].x < 580 ) ? user[id].x + user[id].speed : 580;
        break;
      default:
        break;
    }
};

function User( x, y, width, height )
{
    this.width    = width;
    this.height   = height;
    this.x        = x;
    this.y        = y;
    this.speed    = 2;
    this.catching = false;
}

function getCatchingUser( user )
{
    var catching = false;
    for ( var i = 0; i < user.length; i++ )
    {
        catching = ( user[i].catching ) ? true : false;
    }
    if ( !catching )
    {
        user[0].catching = true;
    }
}

var site = {
    port: 8888,
    host: "www.test1.ru"
};

var io = require( 'socket.io' ).listen( site.port );
io.set( 'log level', 1 );
var user     = new Array();
var pointTag = false;
var id       = 0;

io.sockets.on( 'connection',
function( socket )
{
    user[user.length] = new User( getRandom( 400 ), getRandom( 400 ), 20, 20 )
    getCatchingUser( user );
    socket.emit(           'createUser', user, user.length - 1 );
    socket.broadcast.emit( 'newData',    user )
    id++;
    if ( id > 2 )
    {
        socket.emit( 'notEnter', 'sorry' );
    }

    socket.on( 'keyPress',
    function( x, y, id )
    {
        user[id].x = x;
        user[id].y = y;
        socket.broadcast.emit( 'newData', user );
        socket.emit(           'newData', user );
    } );
    
    setInterval(
    function()
    {
        if ( user.length > 1 )
        {
            if ( intersection( user[0], user[1] ) && user[0].catching && !pointTag )
            {
                user[1].catching = true;
                user[0].catching = false;
                setTimeout( function() { pointTag = true }, 6000 )
            }
            else if ( intersection( user[1], user[0] ) && user[1].catching && pointTag )
            {
                user[1].catching = false;
                user[0].catching = true;
                setTimeout( function() { pointTag = false }, 6000 )
            }
        }
    }, 1000 );

    socket.on( 'disconnect',
    function()
    {
        toLog("disconnect");
        id--;
        user.splice( user.length - 1, 1);
    } );
} );